<?php
require('../include/init.inc.php');
$method = $kd_name = $kd_website = $kd_sn = $fah = $zq = $action = $order_sn = '';
extract($_REQUEST, EXTR_IF_EXISTS);


if (Common::isPost()) {
    if ($action == 'modify') {
        $Order = AdminOrderSn::getSnBySn($order_sn);
        if (empty($Order)) {
            Common::exitWithError(ErrorMessage::ORDER_NOT_EXIST, "snap/order_sn.php");
        }


        if ($zq == 0) {
            $str = "5-7";
        } elseif ($zq == 1) {
            $str = "7-15";
        } else {
            $str = "15及以上";
        }
        $kd = AdminKd::getKdInfoBySn($order_sn);
        if ($kd) {
            $update_data = array("kd_name" => $kd_name, "kd_url" => $kd_website, "kd_sn" => $kd_sn, "isGo" => $fah, "pz_days" => $str, "modifytime" => time());
            $result = AdminKd::update($order_sn, $update_data);
        } else {
            $data = array("kd_name" => $kd_name, "kd_url" => $kd_website, "kd_sn" => $kd_sn, "order_sn" => $order_sn, "isGo" => $fah, "pz_days" => $str, "createtime" => time(), "modifytime" => time());
            $result = AdminKd::add($data);
        }

        if ($result) {
            $orderInfo = AdminOrderSn::getSnBySn($order_sn);
            $kd = AdminKd::getKdInfoBySn($order_sn);

            $no_html = Common::http_get(KD_API_HOST, trim($kd['kd_sn']));//发送请求

            //因为不是数据接口，所以返回的是个html页面 所以要来抓取页面里面的东西
            $zz_res = Common::format_html($no_html, trim($kd['kd_sn']));

            $content = "<p>您的訂單編號：" . $order_sn . "</p>";
            $content .= "<p>快遞公司：" . $kd['kd_name'] . "(" . $kd['kd_url'] . ")</p>";
            $content .= "<p>快遞單號：" . $kd['kd_sn'] . "</p>";
            $content .= "<p>當前快遞狀態：" . $zz_res[1] . "</p>";
            $content .= "<h6>如需查詢商品運送詳情，請登錄黑貓宅急便(http://www.t-cat.com.tw/)訂單查詢頁面</h6>";
            $mail_data = array("to" => $orderInfo['email'], "title" => "物流詳情", "content" => $content);
            Common::sendMail($mail_data);
            echo '<script>alert("向用戶發送物流詳情郵件成功！")</script>';
            SysLog::addLog(UserSession::getUserName(), 'Add', 'KuaidiEdit', $order_sn, json_encode($data));
            Common::exitWithSuccess('修改完成', 'snap/order_sn.php');
        } else {
            OSAdmin::alert("error");
        }
    }
}


$confirm_html = OSAdmin::renderJsConfirm("icon-remove");
Template::assign('osadmin_action_confirm', $confirm_html);
Template::assign('order_sn', $order_sn);
Template::display('snap/kuaidi.tpl');